package com.company.common.function.interceptor.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//alt + insert  override Methods
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截方法访问之前调用");
        String requestURI = request.getRequestURI();
        System.out.println(requestURI + "接口被MyInterceptor拦截了");

        //判断用户是否携带凭证
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            request.getRequestDispatcher("/api/open/unLogin").forward(request, response);
            return false;
        }
        return false;//false 开启连接拦截器  true 不拦截 直接进入接口方法
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("拦截方法访问之后调用");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("整合流程访问之后调用");
    }
}
